// Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview This is the root module for the net-score project.
 * @author gavaletz@google.com (Eric Gavaletz)
 */


goog.provide('resultlist');


goog.require('goog.dom');
goog.require('page');
goog.require('results');
goog.require('track');


/**
 * Indicates that the visualization libraries have been loaded and that their
 * dependencies can start.
 *
 * @type {boolean}
 */
resultlist.visReady = false;


/**
 * Indicates that the data has been loaded.
 *
 * @type {boolean}
 */
resultlist.dataReady = false;


/**
 * Indicates that the results have already been started.
 *
 * Prevents the results from being displayed twice.
 *
 * @type {boolean}
 */
resultlist.resultsDone = false;


/**
 * Represents a result.
 *
 * @typedef {Object.<string, (string|number)>}
 */
resultlist.ResultItem;


/**
 * A list of result headings.
 *
 * @type {Array.<ResultItem>}
 */
resultlist.data;


/**
 * Sets the result data.  Usually just used from the server.
 *
 * @param {Array.<ResultItem>} a the data to be stored.
 */
resultlist.setData = function(a) {
  resultlist.data = a;
  var max_i = resultlist.data.length;
  for (i = 0; i < max_i; i++) {
    //Make the key a link to the detailes results.
    resultlist.data[i]['Key'] = '<a href="' + page.resultDetailUrl +
        resultlist.data[i]['Key'] + '">' + resultlist.data[i]['Key'] + '</a>';

    //Make the date more human friendly.
    resultlist.data[i]['Date'] = new Date(resultlist.data[i]['Date'] *
        1000).toUTCString();
  }

  //Let everyone know that the data is ready to be used.
  resultlist.dataReady = true;

  //If the vis lib is ready then go ahead and display the data.
  if (resultlist.visReady && !resultlist.resultsDone) {
    resultlist.displayResults();
  }
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'resultlistSetData', resultlist.setData);


/**
 * http://code.google.com/apis/loader/
 *
 * This is some tricky stuff and the google.load is not well documented and does
 * some strange things if you are not careful.  Always list the callback
 * function in optional parameters.  See this message for an explination:
 *
 * http://groups.google.com/group/google-maps-api/msg/936506d83fea4458
 */
resultlist.loadLibs = function() {
  google.load('visualization', '1',
      {'callback': resultlist.visualizationLoaded,
        'packages': ['corechart', 'table']});
};


/**
 * The primary entry point for the result list.
 */
resultlist.start = function() {
  track.trackMe(track.trackingCode);
  resultlist.loadLibs();
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'resultlistStart', resultlist.start);


/**
 * Callback function for loading the charts API JavaScript library.
 *
 * Sets the indicator variable letting other functions know that the charts API
 * is loaded and if the test results are complete we can start displaying the
 * results..
 */
resultlist.visualizationLoaded = function() {
  resultlist.visReady = true;
  if (resultlist.dataReady && !resultlist.resultsDone) {
    resultlist.displayResults();
  }
};


/**
 * Displays the list of result items.
 */
resultlist.displayResults = function() {
  resultlist.resultsDone = true;
  page.resultsListSetup();
  var dataObj = results.recordsToObj(resultlist.data);
  var resultsTableDisplayDiv = goog.dom.createDom('div', {'class': 'table'});
  var contentDiv = goog.dom.createDom('div', {}, resultsTableDisplayDiv);
  page.resultsListPopulate(contentDiv);
  results.table(dataObj, resultsTableDisplayDiv);
};
